/**上拉更新内容的实现（开始）**/
var currentPage = 1;
var isLoading = false;
var lastLoadTime = -1;
var adsenseAds = '<li>';
adsenseAds += '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';
adsenseAds += '<ins class="adsbygoogle"';
adsenseAds += '     style="display:block"';
adsenseAds += '     data-ad-client="ca-pub-adsensepubid"';
adsenseAds += '     data-ad-slot="4899790203"';
adsenseAds += '     data-ad-format="auto"></ins>';
adsenseAds += '<script>';
adsenseAds += '(adsbygoogle = window.adsbygoogle || []).push({});';
adsenseAds += '</script>';
adsenseAds += '</li>';
window.onscroll = function() {
	if(lowEnough()){
		var nowTimestamp = Date.parse(new Date());
		var timeGap = nowTimestamp-lastLoadTime;
		//console.log("nowTimestamp="+nowTimestamp+",lastLoadTime="+lastLoadTime+",nowTimestamp-lastLoadTime="+timeGap);
		//console.log($("#loading-warp-end-id").css("display"));
		$("#loading-warp-end-id").css({"display":""});
		//console.log($("#loading-warp-end-id").css("display"));
		if(isLoading==false && timeGap>=1000){
			loadMoreData();
		}
	}
};
function loadMoreData(){
	isLoading=true;
	lastLoadTime = Date.parse(new Date());
	currentPage++;
	//console.log("current page is:"+currentPage);
	loadMore(currentPage);
	isLoading=false;
	$("#loading-warp-end-id").css({"display":"none"});
}
function lowEnough_backup(){
    var pageHeight = Math.max(document.body.scrollHeight,document.body.offsetHeight);
    var viewportHeight = window.innerHeight || 
        document.documentElement.clientHeight ||
        document.body.clientHeight || 0;
    var scrollHeight = window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop || 0;
    // console.log(pageHeight);
    // console.log(viewportHeight);
    // console.log(scrollHeight);
    return pageHeight - viewportHeight - scrollHeight < 20;
}

function lowEnough(){
	var doc_height = $(document).height();
    var scroll_top = $(document).scrollTop(); 
    var window_height = $(window).height();
    //console.log("doc_height:"+doc_height);
    //console.log("scroll_top:"+scroll_top);
    //console.log("window_height:"+window_height);
    //console.log("doc_height-scroll_top-window_height:"+(doc_height-scroll_top-window_height))
    return doc_height-scroll_top-window_height<50 ;
}

function loadMore(currentPage){
	$.ajax({  
        url: '/api/news/getLatestNews/'+currentPage,  
        type: 'GET',  
        dataType: 'json',  
        timeout: 5000,  
        cache: false,  
        beforeSend: LoadFunction, 
        error: errorFunction,  
        success: addContent   
    }) 
}

function LoadFunction() {
    //$("#list").html('Loadin...');
}  
function errorFunction() {  
    //alert("error");  
}
function addContent(resultJson,beforeOrAfter) {                  
    var json = eval(resultJson);  
    var code = json.code;
    var randomInt = Math.trunc(Math.random()*100000);
    var newContent = adsenseAds;
    //console.log("The result code is:"+code);currentPage
    if(code==200){
    	var data = json.data;
    	//console.log("data.length:"+data.length);
    	if(data==null || data.length==0){
    		return;
    	}
	    $.each(data, function (index) {  
	    	if(data[index].addedTime > maxNewsTime){
	    		maxNewsTime = data[index].addedTime;
	    	}
			if(data[index].img!=null && data[index].imgList!=null && data[index].imgList[0].width>400){
				newContent+='<li class="commonBig">';
				newContent+='<a class="thumb" href="'+data[index].url+'" title="'+data[index].title+'">';
				newContent+='<img src="/images/loading.svg" data-original="'+data[index].imgList[0].url+'" alt="'+data[index].title+'" class="lazy" width="320" />';
				newContent+='</a>';
				newContent+='<a class="titleB" href="'+data[index].url+'" title="'+data[index].title+'">';
				newContent+='<h3>';
				newContent+=data[index].title;
				if(data[index].newsType=="VIDEO"){
					newContent+='<span class="videoR sprite"></span>';
				}
				newContent+='<img src="/sitesInfo/favicon?domain='+data[index].urlHost+'" class="icon_img"/>';
				newContent+='</h3>';
				newContent+='</a>';
			}else{
				newContent+='<li>';
				newContent+='<a href="'+data[index].url+'" title="'+data[index].title+'">';
				if(data[index].img!=null && data[index].imgList!=null){
					newContent+='<span class="thumb">';
					newContent+='<img src="/images/loading.svg" data-original="'+data[index].imgList[0].url+'" alt="" class="lazy" width="100" />';
					newContent+='</span>';
				}
				newContent+='<h3>';
				newContent+=data[index].title;
				newContent+='<img src="/sitesInfo/favicon?domain='+data[index].urlHost+'" class="icon_img"/>';
				newContent+='</h3>';
				newContent+='</a>';
			}
	        newContent+='</li>';
	    });  
	    //console.log("newContent:"+newContent);
	    if(beforeOrAfter=="before"){
	    	$("#news_list").prepend(newContent);
	    }else{
	    	$("#news_list").append(newContent);
	    }
	    lazyLoadEvent();
    }
}
/**上拉更新内容的实现（结束）**/

/**下拉更新内容的实现（开始）**/

var currentPullPage = 1;
var $statu = $('.loading-warp .text');
var pullRefresh = $('.wrapper').pPullRefresh({
	$el: $('.wrapper'),
	autoHide: true, 
	$loadingEl: $('.loading-warp'),
	sendData: null,
	//url: '/api/news/getLatestNews/'+currentPage,
	//url: '/api/news/pullLatestNews/'+(currentPage++)+"/"+(currentPullPage++)+"/"+maxNewsTime,
	callbacks: {
		pullStart: function(){
			currentPage++;
			this.url = '/api/news/getLatestNews/'+currentPage;
			//console.log("pull action,current page:"+currentPage+",url:"+this.url);
			$statu.text('Release to refresh.');
		},
		start: function(){
			$statu.text('Data is loading···');
		},
		success: function(response){
			$statu.text('Data load successfully.');
			addContent(response,"before");
		},
		end: function(){
			$statu.text('Data load finished.');
		},
		error: function(){
			currentPage--;
			$statu.text('Refresh failed. Please try again later.');
		}
	}
});

function pullDownSuccFunction(resultJson) {                  
    var json = eval(resultJson);  
    var code = json.code;
    var newContent = '';
    //console.log("The result code is:"+code);
    if(code==200){
    	var data = json.data;
	    $.each(data, function (index) {  
			if(data[index].img!=null && data[index].imgList[0].width>400){
				newContent+='<li class="commonBig">';
				newContent+='<a class="thumb" href="'+data[index].url+'" title="'+data[index].title+'">';
				newContent+='<img src="/images/loading.svg" data-original="'+data[index].imgList[0].url+'" alt="'+data[index].title+'" class="lazy" width="320" />';
				newContent+='</a>';
				newContent+='<a class="titleB" href="'+data[index].url+'" title="'+data[index].title+'">';
				newContent+='<h3>';
				newContent+=data[index].title;
				if(data[index].newsType=="VIDEO"){
					newContent+='<span class="videoR sprite"></span>';
				}
				newContent+='<img src="/sitesInfo/favicon?domain='+data[index].urlHost+'" class="icon_img"/>';
				newContent+='</h3>';
				newContent+='</a>';
			}else{
				newContent+='<li>';
				newContent+='<a href="'+data[index].url+'" title="'+data[index].title+'">';
				if(data[index].img!=null){
					newContent+='<span class="thumb">';
					newContent+='<img src="/images/loading.svg" data-original="'+data[index].imgList[0].url+'" alt="" class="lazy" width="100" />';
					newContent+='</span>';
				}
				newContent+='<h3>';
				newContent+=data[index].title;
				newContent+='<img src="/sitesInfo/favicon?domain='+data[index].urlHost+'" class="icon_img"/>';
				newContent+='</h3>';
				newContent+='</a>';
			}
	        newContent+='</li>';
	    });  
	    //console.log(newContent);
	    $("#news_list").prepend(newContent);
	    lazyLoadEvent();
    }
}